package cn.how2go.net.server.protocol;

import cn.how2go.net.server.util.BinaryUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 位置信息报告
 */
public class JT_0200 implements JTMessage {
    private static Logger logger = LoggerFactory.getLogger(JT_0200.class);
    /**
     * 报警标志
     */
    private int alarmFlag;
    /**
     * 状态
     */
    private int status;
    /**
     * 纬度,以度为单位的纬度值乘以10的6次方，精确到百万分之一度
     */
    private int latitude;
    /**
     * 经度,以度为单位的经度值乘以10的6次方，精确到百万 分之一度
     */
    private int longitude;
    /**
     * 海拔高度，单位为米（m）
     */
    private short altitude;
    /**
     * 速度,1/10km/h
     */
    private short speed;
    /**
     * 方向,0～359，正北为0，顺时针
     */
    private short course;
    /**
     * 时间,yyyy-MM-dd HH:mm:ss（GMT+8时间，本标准中之后涉及的时间均采用此时区）
     */
    private String time;

    private byte[] expand;

    @Override
    public byte[] write() {
        ByteBuf directBuf = Unpooled.directBuffer(16);
        try {
            directBuf.writeInt(alarmFlag);
            directBuf.writeInt(status);
            directBuf.writeInt(latitude);
            directBuf.writeInt(longitude);
            directBuf.writeShort(altitude);
            directBuf.writeShort(speed);
            directBuf.writeShort(course);
            directBuf.writeBytes(BinaryUtil.getByteFromString(time,"yyyy-MM-dd HH:mm:ss","yyMMddHHmmss"));
            directBuf.writeBytes(expand);
        } catch (Exception e) {
            logger.error("<<<<组包失败");
            directBuf.release();
            return null;
        }

        byte[] bytess = new byte[directBuf.readableBytes()];
        directBuf.readBytes(bytess);
        directBuf.release();
        return bytess;
    }

    @Override
    public void read(byte[] bytes) {
        ByteBuf directBuf = Unpooled.directBuffer(16);
        directBuf.writeBytes(bytes);
        try {
            this.alarmFlag = directBuf.readInt();
            this.status = directBuf.readInt();
            this.latitude = directBuf.readInt();
            this.longitude = directBuf.readInt();
            this.altitude = directBuf.readShort();
            this.speed = directBuf.readShort();
            this.course = directBuf.readShort();
            this.time = "20" + BinaryUtil.bcd2Str(directBuf.readByte())+"-"
                    + BinaryUtil.bcd2Str(directBuf.readByte())+"-"
                    + BinaryUtil.bcd2Str(directBuf.readByte())+" "
                    + BinaryUtil.bcd2Str(directBuf.readByte())+":"
                    + BinaryUtil.bcd2Str(directBuf.readByte())+":"
                    + BinaryUtil.bcd2Str(directBuf.readByte());
            expand = new byte[bytes.length-28];
            directBuf.readBytes(expand);
        } catch (Exception e) {
            logger.error("<<<<解包失败" + BinaryUtil.ToHexFormatString(bytes));
            directBuf.release();
        }
        directBuf.release();
    }

    @Override
    public short getBodyLength() {
        short len = 28;
        if(expand!=null){
            len += expand.length;
        }
        return len;
    }

    @Override
    public String toString() {
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append(String.format(
                "报警标示：%1$s,状态：%2$s,经度：%3$s,纬度：%4$s,高度：%5$s,速度：%6$s,时间：%7$s",
                getAlarmFlag(), getStatus(), getLongitude(), getLatitude(),
                getAltitude(), getSpeed(), getTime()));
        return sBuilder.toString();
    }

    public int getAlarmFlag() {
        return alarmFlag;
    }

    public void setAlarmFlag(int alarmFlag) {
        this.alarmFlag = alarmFlag;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getLatitude() {
        return latitude;
    }

    public void setLatitude(int latitude) {
        this.latitude = latitude;
    }

    public int getLongitude() {
        return longitude;
    }

    public void setLongitude(int longitude) {
        this.longitude = longitude;
    }

    public short getAltitude() {
        return altitude;
    }

    public void setAltitude(short altitude) {
        this.altitude = altitude;
    }

    public short getSpeed() {
        return speed;
    }

    public void setSpeed(short speed) {
        this.speed = speed;
    }

    public short getCourse() {
        return course;
    }

    public void setCourse(short course) {
        this.course = course;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public byte[] getExpand() {
        return expand;
    }

    public void setExpand(byte[] expand) {
        this.expand = expand;
    }
}
